/*
排序案例
案例描述：将Person自定义数据类型进行排序，Person中属性有姓名、年龄、身高

排序规则：按照年龄进行升序，如果年龄相同按照身高进行降序
*/

#include <iostream>
#include <string>
#include <list>
using namespace std;

class Person
{
public:
    Person(string name, int age, int height) : m_name(name), m_age(age), m_height(height) {};

public:
    string m_name;
    int m_age;
    int m_height;
};

bool compare_person(const Person &p1, const Person &p2)
{
    if (p1.m_age == p2.m_age)
        return p1.m_height > p2.m_height; // 升高降序
    return p1.m_age < p2.m_age;           // 年龄升序
}

int main()
{
    Person p1("alias", 20, 180);
    Person p2("alex", 22, 183);
    Person p3("jake", 20, 170);
    Person p4("jordan", 21, 180);
    Person p5("ieven", 21, 170);

    list<Person> lst;
    lst.push_back(p1);
    lst.push_back(p2);
    lst.push_back(p3);
    lst.push_back(p4);
    lst.push_back(p5);

    for (list<Person>::iterator it = lst.begin(); it != lst.end(); it++)
        cout << "name: " << it->m_name << " age: " << it->m_age << " height: " << it->m_height << endl;

    cout << "----------- sort -----------" << endl;

    lst.sort(compare_person);

    for (list<Person>::iterator it = lst.begin(); it != lst.end(); it++)
        cout << "name: " << it->m_name << " age: " << it->m_age << " height: " << it->m_height << endl;
}